package com.logica.mdm.webfrontend.embedding.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logica.mdm.webfrontend.embedding.EmbeddingAdapter;

/**
 * EmbeddingResolver implementation that uses an attribute in the servlet request,
 * with a fallback to the default embedding adapter if missing.
 * @author Fabien Crespel <fabien.crespel@logica.com>
 */
public class RequestEmbeddingResolver extends AbstractEmbeddingResolver {
	
	/**
	 * Name of the request attribute that holds the embedding adapter.
	 */
	public static final String EMBEDDING_ADAPTER_ATTRIBUTE = "embeddingAdapter";

	@Override
	public EmbeddingAdapter resolveEmbeddingAdapter(HttpServletRequest request) {
		EmbeddingAdapter adapter = (EmbeddingAdapter) request.getAttribute(EMBEDDING_ADAPTER_ATTRIBUTE);
		return (adapter != null ? adapter : getDefaultEmbeddingAdapter());
	}

	@Override
	public void setEmbeddingAdapter(HttpServletRequest request, HttpServletResponse response, EmbeddingAdapter embeddingAdapter) {
		request.setAttribute(EMBEDDING_ADAPTER_ATTRIBUTE, embeddingAdapter);
	}

}
